Skip to content

Java: Exclude unnamed variables from java/local-variable-is-never-read - #22994

Merged
aschackmull merged 1 commit into
github:mainfrom
riemenschneider:java/unread-local-unnamed-variables
Nov 3, 2026
Merged

aschackmull merged 1 commit into
github:mainfrom
riemenschneider:java/unread-local-unnamed-variables

Conversation

@riemenschneider

@riemenschneider riemenschneider commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #22993.

java/local-variable-is-never-read reports unnamed variables (_, Java 22), for example case Foo _ ->. An unnamed variable cannot be read, so every such alert is a false positive.

The query now skips variables whose declaration isAnonymous(). The source version already decides which variables that matches, so the query needs no version check of its own. Before Java 9, _ is an ordinary identifier: the extractor keeps its name, and an unread _ is still reported. Java 9 to 20 reject _ as a name. From Java 22, or Java 21 with preview features, _ declares an unnamed variable, which the extractor gives an empty name.

UnnamedVariables.java covers unnamed variables in switch type patterns, record patterns, an enhanced for loop, and a local declaration. The new options file compiles the test directory with --release 22, as printAst and pattern-switch/cfg already do. None of the new cases produce a result, so the three .expected files in the directory are unchanged.

Tested locally with CodeQL CLI 2.27.4. Without the query change, UnreadLocal.qlref fails with five unexpected alerts, one per case in UnnamedVariables.java. With it, all three tests in the directory pass. A separate run with --release 8 confirmed that an unread int _ = 5; is still reported.

🤖 Generated with Claude Code

An unnamed variable (`_`, Java 22) cannot be read, so reporting it is
always a false positive.

Fixes github#22993.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI auto review requested due to automatic review settings November 3, 2026 11:03
@riemenschneider
riemenschneider requested a review from a team as a code owner November 3, 2026 11:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are identified for the targeted Java 22 unnamed-variable query update.

No open issues 🎉

What changed in this PR

Updates the Java local-variable-is-never-read query to ignore Java 22 unnamed variables, which cannot be read and should not produce alerts.

Changes:

  • Excludes anonymous local variable declarations from query results.
  • Adds Java 22 regression coverage for unnamed variables in patterns, enhanced for loops, and local declarations.
  • Configures the test directory for Java 22 compilation.
  • Documents the behavior change.
File Description
java/​ql/​test/​query-tests/​UnreadLocal/​UnreadLocal/​UnnamedVariables.java Adds Java 22 regression cases for unnamed variables.
java/​ql/​test/​query-tests/​UnreadLocal/​options Enables Java 22 compilation for the test directory.
java/​ql/​src/​Violations of Best Practice/​Dead Code/​UnreadLocal.ql Excludes unnamed variables from query results.
java/​ql/​src/​change-notes/​2026-11-03-unread-local-unnamed-variables.md Documents the query behavior change.

This pull request is ready to be approved.

Review effort: Lite (auto)

@aschackmull aschackmull left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the contribution!

@aschackmull
aschackmull merged commit 915bfa8 into github:main Nov 3, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Java: java/local-variable-is-never-read reports unnamed variables (_)

3 participants